(function() {
	function getNode(node){
		var nodeExpr=node.tagName;
		if(nodeExpr==null)
			return null;
		if(node.id!=""){
			nodeExpr+="[@id=\""+node.id+"\"]";
			return "/"+nodeExpr;
		}
		var rank=1;
		var ps=node.previousSibling;
		while(ps!=null){
			if(ps.tagName==node.tagName){
				rank++;
			}
			ps=ps.previousSibling;
		}
		if(rank>1){
			nodeExpr+="["+rank+"]";
		} else {
			var ns=node.nextSibling;
			while(ns!=null){
				if(ns.tagName==node.tagName){
					nodeExpr+="[1]";
					break;
				}
				ns=ns.nextSibling;
			}
		}
		return nodeExpr;
	}
	
	function getXpath() {
		var currentNode=window.getSelection().anchorNode;
		if(currentNode==null){
			return "";
		}
		var path=[];
		while(currentNode!=undefined){
			var pe=getNode(currentNode);
			if(pe!=null){
				path.push(pe);
				if(pe.indexOf("@id")!=-1)
					break;
			}
			currentNode=currentNode.parentNode;
		}
		var xpath="/"+path.reverse().join("/");
		return xpath;
	}
	
	var url = encodeURIComponent(window.location.href),
	xpath = encodeURIComponent(getXpath()),
	token = encodeURIComponent("__token__"),
	popup = window.open('__path__/getReferences.html?url='+url+'&xpath='+xpath+'&token='+token, 'ref-win', 'dependent=yes,height=400,menubar=no,location=no,resizable=yes,scrollbars=no,status=no,toolbar=no,width=250');
})();